<?php



function show_page() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;
  if (isset($data["type"]) && $data["type"] >= 0) {
    if (!isset($_GET["act"])) $_GET["act"] = "page";
    switch ($_GET["act"]) {
    	
	  case "wslistdata":
		  include("web_services.php");
		  if (intval($_GET['wsid'])) {
			  $contents = getWSData ($_GET['wsid']);
		  } else {
			  $contents = weightFunction (substr($_GET['uid'], 2));
		  }
	  break;
		  
	  case "updateFL":
		  include("web_services.php");
		  $contents = getFL (intval($_GET['wsid']), $_REQUEST['sname']);
	  break;
		  
	  case "setFunction":
		  include("web_services.php");
		  $contents = setFunction (intval($_GET['wsid']), trim($_REQUEST['sname']), trim($_REQUEST['fname']));
	  break;
		  
	  case "wslist":
			include("web_services.php");
			$contents = WSList (substr($_GET['uid'], 1));
  	  break;
  	  
      case "vrml":
        include "vrml.php";
        $contents = show_vrml();
        break;
      case "tax":
        include "class.php";
        $contents = taxonomy(1);
        break;
      case "2dview":
        include "class.php";
        $contents = taxonomy(0);
        break;
      case "attrdata":
        include "attr.php";
        $contents = attrData();
        break;
      case "attrlist":
        include "attr.php";
        $contents = attrList(0);
        break;
      case "iattrlist":
        include "attr.php";
        $contents = attrList(1);
        break;
      case "oattrlist":
        include "attr.php";
        $contents = attrList(2);
        break;
      case "attrlistdata":
        include "attr.php";
        $contents = AttrListFolderData(getMessage("Attributes"));
        break;
      case "attralignment":
        include "alignment.php";
        $contents = attrAlignment();
        break;
      case "alignmentdata":
        include "alignment.php";
        $contents = alignmentData();
        break;
      case "alignmentlist":
        include "alignment.php";
        $contents = alignmentList();
        break;
      case "alignmentlistdata":
        include "alignment.php";
        $contents = alignmentListFolderData(getMessage("Alignment"));
        break;
      case "aconlist":
        include "acon.php";
        $contents = aconList();
        break;
      case "acondata":
        include "acon.php";
        $contents = aconData(-1);
        break;
      case "aconfunctional":
        include "acon.php";
        $contents = acon_functional();
        break;
      case "aconlistdata":
        include "acon.php";
        $contents = AconListFolderData(getMessage("Associative Constraints"));
        break;
      case "compatiblelist":
        include "compatible.php";
        $contents = compatibleList();
        break;
      case "compatibledata":
        include "compatible.php";
        $contents = compatibleData(-1);
        break;
      case "compatiblelistdata":
        include "compatible.php";
        $contents = CompatibleListFolderData(getMessage("Compatibility"));
        break;
      case "hierarchylist":
        include "hierarchy.php";
        $contents = hierarchyList();
        break;
      case "hierarchydata":
        include "hierarchy.php";
        $contents = hierarchyData(-1, -1, -1);
        break;
      case "hierarchylistdata":
        include "hierarchy.php";
        $contents = hierarchyListFolderData(getMessage("Hierarchy"));
        break;
      case "hierarchyfunctional":
        include "hierarchy.php";
        $contents = hierarchy_functional();
        break;
      case "functionallistdata":
        include "functional.php";
        $contents = functionalListFolderData(getMessage("Functional constraints"));
        break;
      case "functionaldata":
        include "functional.php";
        $contents = functionalData(-1);
        break;
      case "getfunctionalattrs":
        include "functional.php";
        $contents = getfunctionalattrs();
        break;
      case "functionalattributedata":
        include "functional.php";
        $contents = functionalattributeData();
        break;
      case "functionallist":
        include "functional.php";
        $contents = functionalList();
        break;
      case "synonymlist":
        include "synonyms.php";
        $contents = synonymList();
        break;
      case "synonymlistdata":
        include "synonyms.php";
        $contents = synonymListFolderData();
        break;
      case "synonymdata":
        include "synonyms.php";
        $contents = synonymData(-1);
        break;
      case "classdata":
        include "class.php";
        $contents = classData();
        break;
      case "ontodata":
        $contents = getOntoData(1, "");
        header ($header["xml"]);
        break;
      case "ontolist":
        $filename = $path[$style]["tpl"]."/ontos.xml";
        $fp = fopen($filename, "r");
        $contents = fread($fp, filesize ($filename));
        fclose ($fp);

        preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
        $rep_cont = "";
        $i_max = getMultData("SELECT ontology.id as id, term.text as name ".
          "FROM ontology, term ".
          "WHERE ontology.term_id = term.id AND term.lang_id = ".$language["content"].
          " ORDER BY term.text;",
          $ontosdata);

        //Ontos Data
        for ($i = 0; $i < $i_max; $i++) {
          $tmp_cont = str_replace("%ONTO_ID%", $ontosdata[$i]["id"], $matches[1]);
          $tmp_cont = str_replace("%ONTO_NM%", $ontosdata[$i]["name"], $tmp_cont);
          $tmp_cont = str_replace("%IMG_TYPE%", "ontology", $tmp_cont);
          $tmp_cont = str_replace("%ONTO_LN%", "index.php?trg=ontoedit&amp;act=ontodata&amp;sid=".
            $session_id."&amp;uid=".$ontosdata[$i]["id"], $tmp_cont);
          //if (isset($_GET["sel"]) && $_GET["sel"] == "0" && $i == 0)
          if ((!isset($_GET["sel"]) || (isset($_GET["sel"]) && $_GET["sel"] == "0")) && $i == 0)
            $tmp_cont = str_replace("%ACT%", "select", $tmp_cont);
          if (isset($_GET["sel"]) && $_GET["sel"] == $ontosdata[$i]["id"])
            $tmp_cont = str_replace("%ACT%", "select", $tmp_cont);
          $rep_cont .= $tmp_cont;
        }
        $contents = str_replace($matches[0], $rep_cont, $contents);
        header ($header["xml"]);
        break;
      case "update":
        $comment = "";
        switch (substr($_GET["uid"], 0, 1)) {
          case "a":
            if (substr($_GET["uid"], 0, 2) == "ac"){
              include "acon.php";
              $contents = updateAcon();
            }else{
              if (substr($_GET["uid"], 0, 2) == "al"){
                include "alignment.php";
                $contents = updateAlignment();
              }else{
                include "attr.php";
                $contents = updateAttr();
              };
            };
            break;
          case "i":
            include "attr.php";
            $contents = updateAttr();
            break;
          case "o":
            include "attr.php";
            $contents = updateAttr();
            break;
          case "c":
            if (substr($_GET["uid"], 0, 3) == "com"){
              include "compatible.php";
              $contents = updateCompatible();
            }else{
              include "class.php";
              $contents = updateClass();
            };
            break;
          case "h":
            include "hierarchy.php";
            $contents = updateHierarchy();
            break;
          case "w":
          	include "web_services.php";
      		if (!isset($_REQUEST['cid'])) {
	          	$wp = array();
	          	foreach ($_REQUEST as $k => $v) {
	          		if (substr($k, 0, 2) == "wp") {
	          			$wp[$k] = utf8Encode ($v);
	          		}
	          	}
	          	$contents = bindAttr(utf8Encode ($_REQUEST['wsid']), $wp);
          	} else {
          		if (isset($_REQUEST['wssid'])) {
          			$contents = applyWSS (utf8Encode($_REQUEST['cid']), utf8Encode($_REQUEST['wssid']));
          		} else {
          			$p = array();
		          	foreach ($_REQUEST as $k => $v) {
		          		if (substr($k, 0, 1) == "p") {
		          			$p[substr($k, 1)] = utf8Encode ($v);
		          		}
		          	}
		          	$contents = saveWF(utf8Encode ($_REQUEST['cid']), $p);
          		}
          	}
          	
          	break;
          case "f":
            if (substr($_GET["uid"], 0, 3) == "fun"){
              if (strpos(substr($_GET["uid"], 4), "a") > 0) {
                include "functional.php";
                $contents = updatefunctionalattr();
                break;
              } else {
                include "functional.php";
                $contents = updatefunctional();
                break;
              };
            };
          case "s":
            if (substr($_GET["uid"], 0, 3) == "syn"){
              include "synonyms.php";
              $contents = updatesynonym();
              break;
            };
          default:
            if ($data["type"] >= 1 && isset($_GET["oname"]) && isset($_GET["odescr"]) && isset($_GET["otype"])) {
              $_GET["oname"] = utf8Encode($_GET["oname"]);
              $_GET["odescr"] = utf8Encode($_GET["odescr"]);
              $_GET["otype"] = utf8Encode($_GET["otype"]);
              getData("SELECT term_id FROM ontology WHERE id = ".$_GET["uid"].";", $ontoterm);
              if ( $_GET["oname"] == "") {
                $comment = getMessage("EmptyName");
              } else {
                $sql_str = "UPDATE term SET term.text = '".$_GET["oname"].
                  "', term.description = '".$_GET["odescr"].
                  "' WHERE term.id = ".$ontoterm["term_id"].
                  " AND term.lang_id = ".$language["content"].";";
                insertRecord($sql_str);
                $sql_str = "UPDATE ontology SET ontology.type_id = ".$_GET["otype"].
                  " WHERE ontology.id = ".$_GET["uid"].";";
                insertRecord($sql_str);
              }
            }
            $contents = getOntoData(0, $comment);
            break;
        }
        header ($header["xml"]);
        break;
      case "delete": //user's data
        switch (substr($_GET["uid"], 0, 1)) {
          case "a":
            if (substr($_GET["uid"], 0, 2) == "ac"){
              include "acon.php";
              $_GET["uid"] = substr($_GET["uid"], 2);
              $ID = explode("ac", $_GET["uid"]);
              include "compatible.php";
              $contents = deleteAcon($ID);
            }else{
              if (substr($_GET["uid"], 0, 2) == "al"){
                include "alignment.php";
                $ID = Array(); $ID[0] = -1;
                $tmp_uid = $_GET["uid"];
                $_GET["uid"] = substr($_GET["uid"], 3);
                if (strpos($_GET["uid"], "alc") != false){
                  $ID = explode("alc", $_GET["uid"]);
                  $ID[2] = "alc";
                };
                if (strpos($_GET["uid"], "ala") != false){
                  $ID = explode("ala", $_GET["uid"]);
                  $ID[2] = "ala";
                };
                if (substr($tmp_uid, 0, 3) == "alc")
                  $ID[3] = "alc";
                else
                  $ID[3] = "ala";
                $contents = deleteAlignment($ID);
              }else{
                include "attr.php";
                $_GET["uid"] = substr($_GET["uid"], 1);
                $ID = explode("a", $_GET["uid"]);
                $contents = deleteAttr($ID, 0);
              };
            };
            break;
          case "i":
            include "attr.php";
            $_GET["uid"] = substr($_GET["uid"], 2);
            $ID = explode("ia", $_GET["uid"]);
            $contents = deleteAttr($ID, 1);
            break;

          case "o":
            include "attr.php";
            $_GET["uid"] = substr($_GET["uid"], 2);
            $ID = explode("oa", $_GET["uid"]);
            $contents = deleteAttr($ID, 2);
            break;

          case "c":
            if (substr($_GET["uid"], 0, 3) == "com"){
              include "compatible.php";
              $_GET["uid"] = substr($_GET["uid"], 3);
              $ID = explode("com", $_GET["uid"]);
              $contents = deleteCompatible($ID);
            }else{
              include "class.php";
              $contents = deleteClass();
            };
            break;
          case "h":
            include "hierarchy.php";
            include "compatible.php";
            $_GET["uid"] = substr($_GET["uid"], 1);
            $ID = explode("h", $_GET["uid"]);
            $contents = deleteHierarchy($ID);
            break;
          case "f":
            if ((substr($_GET["uid"], 0, 3) == "fun") && (is_numeric(substr($_GET["uid"], 4)))){
              //echo substr($_GET["uid"], 4);
              include "functional.php";
              $contents = deleteFunctional();
              break;
            }else{
              $filename = $path[$style]["tpl"]."/action.xml";
              $fp = fopen($filename, "r");
              $contents = fread($fp, filesize ($filename));
              $contents = str_replace("%SCCSS%", "0", $contents);
              fclose ($fp);
              preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
            };
            break;
          case "s":
            if ((substr($_GET["uid"], 0, 3) == "syn") && (is_numeric(substr($_GET["uid"], 3)))){
              include "synonyms.php";
              $contents = deleteSynonym();
              break;
            }else{
              $filename = $path[$style]["tpl"]."/action.xml";
              $fp = fopen($filename, "r");
              $contents = fread($fp, filesize ($filename));
              $contents = str_replace("%SCCSS%", "0", $contents);
              fclose ($fp);
              preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
            };
            break;
          case "w":
          	if (substr($_GET['uid'], 0, 10) == "wslistdata" && is_numeric(substr($_GET['uid'], 10))) {
          		include("web_services.php");
          		$contents = '';deleteWebService (intval(substr($_GET['uid'], 10)));
          	}
          break;
          default:
            $filename = $path[$style]["tpl"]."/action.xml";
            $fp = fopen($filename, "r");
            $contents = fread($fp, filesize ($filename));
            fclose ($fp);
            preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
            if ($data["type"] >= 1) {
              if (isset($_GET["uid"])) {
                //delete classes
                include "class.php";
                if (getData("SELECT id FROM class WHERE onto_id = ".$_GET["uid"].
                  " AND parent_id = 0;", $ontosdata) &&
                  deleteClassID($ontosdata["id"], 1) &&
                  getData("SELECT term_id FROM ontology WHERE ontology.id = ".$_GET["uid"].";",
                    $ontodata) == 1) {
                  insertRecord("DELETE FROM ontology WHERE id=".$_GET["uid"].";");
                  insertRecord("DELETE FROM term WHERE id=".$ontodata["term_id"].";");
                  $contents = str_replace("%SCCSS%", "1", $contents);
                }
                else $contents = str_replace("%SCCSS%", "0", $contents);
              }
              else $contents = str_replace("%SCCSS%", "0", $contents);
            }
            else $contents = str_replace("%SCCSS%", "0", $contents);

            $contents = str_replace($matches[0], "", $contents);
            $contents = str_replace("%ACT_ID%", "Delete", $contents);
            $contents = str_replace("%PARENT%", $_GET["uid"], $contents);
            break;
        }
        header ($header["xml"]);
        break;
      case "addattr":
        include "attr.php";
        $contents = createAttr();
        break;
      case "search":
        include "search.php";
        $contents = searchelement();
        break;
      case "addnew":
        //print ($_GET["uid"]); $contents = ""; break;
        switch (substr($_GET["uid"], 0, 1)) {
          case "c":
            if (substr($_GET["uid"], 0, 3) == "com"){
              include "compatible.php";
              $contents = createCompatible();
            }else{
              include "class.php";
              $contents = createClass();
            };
            break;
          case "a":
            if (substr($_GET["uid"], 0, 2) == "ac"){
              include "acon.php";
              $contents = createAcon();
            }else{
              if (substr($_GET["uid"], 0, 2) == "al"){
                include "alignment.php";
                $contents = createAlignment();
              }else{
                include "attr.php";
                $contents = createAttr();
              };
            };
            break;
          case "i":
            if (substr($_GET["uid"], 0, 2) == "ia"){
              include "attr.php";
              $contents = createIOAttr(1);
            }
            break;
          case "o":
            if (substr($_GET["uid"], 0, 2) == "oa"){
              include "attr.php";
              $contents = createIOAttr(2);
            }else{
              //add class upper level in AO
              include "class.php";
              $contents = createClass();
            };
            break;
          case "h":
            include "hierarchy.php";
            $contents = createHierarchy();
            break;
          case "f":
            if ((substr($_GET["uid"], 0, 3) == "fun") && (substr($_GET["uid"], 4, 4) == "list")){
                include "functional.php";
                $contents = createFunctional();
                break;
            }
          case "s":
            if (substr($_GET["uid"], 0, 7) == "synlist"){
                include "synonyms.php";
                $contents = createSynonym();
                break;
            }
          case "w":
          	if (substr($_GET['uid'], 0, 6) == "wslist") {
          		include "web_services.php";
          		$contents = createWebService (substr($_GET['uid'], 6), true);
          		break;
          	}
          break;
          default:
            //print ($_GET["uid"]); $contents = ""; break;
            if ($_GET["uid"] > 0){
              if ($data["type"] >= 1) {
                $filename = $path[$style]["tpl"]."/action.xml";
                $fp = fopen($filename, "r");
                $contents = fread($fp, filesize ($filename));
                fclose ($fp);
                preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
                $sql_str = "INSERT INTO term ([lang_id], [text], [description]) VALUES(".
                  $language["content"].",'".
                  $session_id."','".$session_id."');";
                //print ($sql_str);
                insertRecord($sql_str);
                $sql_str = "SELECT id FROM term where text = '".
                  $session_id."' AND description = '".$session_id.
                  "' AND term.lang_id = ".$language["content"].
                  ";";
                //print ($sql_str);
                if (getData($sql_str, $datatmp) == 1) {
                  $sql_str = "UPDATE term SET term.text = '".getMessage("Name").
                    "', term.description = '".getMessage("Description").
                    "' WHERE term.id=".$datatmp["id"].";";
                  insertRecord($sql_str);
                  //print ($sql_str);
                  insertRecord("INSERT INTO ontology (term_id, type_id, session_id) VALUES(".
                    $datatmp["id"].", 1, '".
                    $session_id."');");
                  if (getData("SELECT id FROM ontology where session_id = '".
                    $session_id."';", $datatmp) == 1) {
                    $sql_str = "UPDATE ontology SET ontology.session_id = '' WHERE id=".
                    $datatmp["id"].";";
                    insertRecord($sql_str);
                    insertRecord("INSERT INTO class (onto_id, parent_id, base_onto_id, term_id, session_id) VALUES(".
                      $datatmp["id"].", 0, 0, ".$term_Thing_ID.", '');");
                    $contents = str_replace("%SCCSS%", "1", $contents);
                  } else {
                    $contents = str_replace("%SCCSS%", "0", $contents);
                    $datatmp["id"] = 0;
                  }
                } else {
                  $contents = str_replace("%SCCSS%", "0", $contents);
                  $datatmp["id"] = 0;
                }
              } else {
                $contents = str_replace("%SCCSS%", "0", $contents);
                $datatmp["id"] = 0;
              }
              $tmp_cont = str_replace("%NODE_ID%", $datatmp["id"], $matches[1]);
              $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
              $tmp_cont = str_replace("%IMG_TYPE%", "ontology", $tmp_cont);
              $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=ontodata&amp;sid=".
                $session_id."&amp;uid=".$datatmp["id"], $tmp_cont);

              $contents = str_replace($matches[0], $tmp_cont, $contents);
              $contents = str_replace("%ACT_ID%", "Add", $contents);
              $contents = str_replace("%PARENT%", "root", $contents);
            }else{
                $filename = $path[$style]["tpl"]."/action.xml";
                $fp = fopen($filename, "r");
                $contents = fread($fp, filesize ($filename));
                fclose ($fp);
            };
            break;
        }
        header ($header["xml"]);
        break;
      default: //including "page"
        $filename = $path[$style]["tpl"]."/ontoedit.htm";
        $fp = fopen($filename, "r");
        $contents = fread($fp, filesize ($filename));
        fclose ($fp);

        $contents = str_replace("%ROOT_CSS%", $path[$style]["css"], $contents);
        $contents = str_replace("%ROOT_IMG%", $path[$style]["img"], $contents);
        $contents = str_replace("%ROOT_JAVA%", $path[$style]["java"], $contents);

        $contents = str_replace("%SID%", $session_id, $contents);
        $contents = str_replace("%TRG%", "ontoedit", $contents);

        $contents = str_replace("%LOGOUT%", getMessage("Logout"), $contents);
        $contents = str_replace("%MAINMENU%", getMessage("Main menu"), $contents);
        $contents = str_replace("%ONTOEDIT%", getMessage("Ontology editor"), $contents);
        $contents = str_replace("%ADD%", getMessage("Add"), $contents);
        $contents = str_replace("%DEL%", getMessage("Delete"), $contents);
        $contents = str_replace("%SEARCH%", getMessage("Search Button"), $contents);
        $contents = str_replace("%LOAD%", getMessage("Loading"), $contents);
        $contents = str_replace("%UNAVAIL%", getMessage("Unavailable"), $contents);
        header ($header["html"]);
        break;
    }
    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
    header ("Pragma: no-cache");                          // HTTP/1.0

    print ($contents);
  }
  else { //no rights
    header("Location: index.php?trg=main&sid=".$session_id);
  }
}

function getOntoData($buttons, $comment) {
  global $_GET, $path, $style, $session_id, $data, $language;
  $filename = $path[$style]["tpl"]."/ontodata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";

  if (substr($_GET["uid"], 0, 1) == "o")
    //AO ontology
    $ontology_id = substr($_GET["uid"], 1, strpos($_GET["uid"], "c") - 1);
  else
    $ontology_id = $_GET["uid"];

  getData("SELECT term.text as name, term.description as description, ontology.type_id as type ".
          "FROM ontology, term ".
          "WHERE ontology.id = ".$ontology_id.
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].";",
          $ontodata);
  $i_max = getMultData("SELECT ontotype.id as id, term.text as name FROM ontotype, term ".
          "WHERE ontotype.term_id = term.id AND term.lang_id = ".$language["content"].";",
          $ontotypes);

  $udata = ""; $btns = "";
  if ($buttons == 1) {
    // ask for user of which constraints to show
    if (($ontodata["type"] == 0) || ($ontodata["type"] == 2)){
      $chkbxiattr = str_replace($optmatches[0], "", $datmatches[1]);
      $chkbxiattr = str_replace("%TYPE%", "CheckBox", $chkbxiattr);
      $chkbxiattr = str_replace("%NODE_ID%", "oiattr", $chkbxiattr);
      $chkbxiattr = str_replace("%NODE_TL%", getMessage("Input attributes"), $chkbxiattr);
      $chkbxiattr = str_replace("%VALUE%", 1, $chkbxiattr);

      $chkbxoattr = str_replace($optmatches[0], "", $datmatches[1]);
      $chkbxoattr = str_replace("%TYPE%", "CheckBox", $chkbxoattr);
      $chkbxoattr = str_replace("%NODE_ID%", "ooattr", $chkbxoattr);
      $chkbxoattr = str_replace("%NODE_TL%", getMessage("Output attributes"), $chkbxoattr);
      $chkbxoattr = str_replace("%VALUE%", 1, $chkbxoattr);
    }else{
      $chkbxiattr = "";
      $chkbxoattr = "";
    };
    $chkbxattrs = str_replace($optmatches[0], "", $datmatches[1]);
    $chkbxattrs = str_replace("%TYPE%", "CheckBox", $chkbxattrs);
    $chkbxattrs = str_replace("%NODE_ID%", "oattr", $chkbxattrs);
    $chkbxattrs = str_replace("%NODE_TL%", getMessage("Attributes"), $chkbxattrs);
    $chkbxattrs = str_replace("%VALUE%", 1, $chkbxattrs);

    $chkbxacon = str_replace($optmatches[0], "", $datmatches[1]);
    $chkbxacon = str_replace("%TYPE%", "CheckBox", $chkbxacon);
    $chkbxacon = str_replace("%NODE_ID%", "oacon", $chkbxacon);
    $chkbxacon = str_replace("%NODE_TL%", getMessage("Associative Constraints"), $chkbxacon);
    $chkbxacon = str_replace("%VALUE%", 1, $chkbxacon);

    if ($ontodata["type"] != 2){
      $chkbxcomp = str_replace($optmatches[0], "", $datmatches[1]);
      $chkbxcomp = str_replace("%TYPE%", "CheckBox", $chkbxcomp);
      $chkbxcomp = str_replace("%NODE_ID%", "ocomp", $chkbxcomp);
      $chkbxcomp = str_replace("%NODE_TL%", getMessage("Compatibility"), $chkbxcomp);
      $chkbxcomp = str_replace("%VALUE%", 1, $chkbxcomp);
    }else{
      $chkbxcomp = "";
    };
    $chkbxh = str_replace($optmatches[0], "", $datmatches[1]);
    $chkbxh = str_replace("%TYPE%", "CheckBox", $chkbxh);
    $chkbxh = str_replace("%NODE_ID%", "oh", $chkbxh);
    $chkbxh = str_replace("%NODE_TL%", getMessage("Hierarchy"), $chkbxh);
    $chkbxh = str_replace("%VALUE%", 1, $chkbxh);

    if ($ontodata["type"] == 0){
      $chkbxfunc = str_replace($optmatches[0], "", $datmatches[1]);
      $chkbxfunc = str_replace("%TYPE%", "CheckBox", $chkbxfunc);
      $chkbxfunc = str_replace("%NODE_ID%", "ofunc", $chkbxfunc);
      $chkbxfunc = str_replace("%NODE_TL%", getMessage("Functional constraints"), $chkbxfunc);
      $chkbxfunc = str_replace("%VALUE%", 1, $chkbxfunc);
    }else{
      $chkbxfunc = "";
    };
    if ($ontodata["type"] == 5){
      $chkbxalignment = str_replace($optmatches[0], "", $datmatches[1]);
      $chkbxalignment = str_replace("%TYPE%", "CheckBox", $chkbxalignment);
      $chkbxalignment = str_replace("%NODE_ID%", "oalignment", $chkbxalignment);
      $chkbxalignment = str_replace("%NODE_TL%", getMessage("Alignment"), $chkbxalignment);
      $chkbxalignment = str_replace("%VALUE%", 1, $chkbxalignment);
    }else{
      $chkbxattr = "";
    };
    $chkbxsynonyms = str_replace($optmatches[0], "", $datmatches[1]);
    $chkbxsynonyms = str_replace("%TYPE%", "CheckBox", $chkbxsynonyms);
    $chkbxsynonyms = str_replace("%NODE_ID%", "osyn", $chkbxsynonyms);
    $chkbxsynonyms = str_replace("%NODE_TL%", getMessage("Synonyms"), $chkbxsynonyms);
    $chkbxsynonyms = str_replace("%VALUE%", 1, $chkbxsynonyms);
    
    if (in_array($ontodata["type"], array(0, 2))){
	    $chkbxws = str_replace($optmatches[0], "", $datmatches[1]);
	    $chkbxws = str_replace("%TYPE%", "CheckBox", $chkbxws);
	    $chkbxws = str_replace("%NODE_ID%", "ows", $chkbxws);
	    $chkbxws = str_replace("%NODE_TL%", getMessage("Web-Services"), $chkbxws);
	    $chkbxws = str_replace("%VALUE%", 1, $chkbxws);
    }

    // class_operations
    $chkbxcop = str_replace($optmatches[0], "", $datmatches[1]);
    $chkbxcop = str_replace("%TYPE%", "CheckBox", $chkbxcop);
    $chkbxcop = str_replace("%NODE_ID%", "ocop", $chkbxcop);
    $chkbxcop = str_replace("%NODE_TL%", getMessage("Class Operations"), $chkbxcop);
    $chkbxcop = str_replace("%VALUE%", 0, $chkbxcop);

    $chks = str_replace($datmatches[0], $chkbxiattr.$chkbxoattr.$chkbxattrs.$chkbxacon.$chkbxcomp.$chkbxh.$chkbxfunc.$chkbxalignment.$chkbxsynonyms.$chkbxws.$chkbxcop, $matches[1]);
    $chks = str_replace("%GROUP_ID%", "2", $chks);
    $chks = str_replace("%GROUP_TL%", getMessage("View settings"), $chks);

    $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
    $btn1 = str_replace("%TYPE%", "Button", $btn1);
    $btn1 = str_replace("%NODE_ID%", "", $btn1);
    $btn1 = str_replace("%NODE_TL%", getMessage("Taxonomy"), $btn1);
    $btn1 = str_replace("%VALUE%", "'p_wd_tree', 'index.php?trg=ontoedit&amp;act=tax&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."&amp;sel=0','oiattr','ooattr','oattr','oacon','ocomp','oh','ofunc','oalignment', 'osyn', 'ocop', 'ows'", $btn1);
    //VRML
    $btn2 = str_replace($optmatches[0], "", $datmatches[1]);
    $btn2 = str_replace("%TYPE%", "Button", $btn2);
    $btn2 = str_replace("%NODE_ID%", "", $btn2);
    $btn2 = str_replace("%NODE_TL%", getMessage("VRML"), $btn2);
    $btn2 = str_replace("%VALUE%", "'new_wnd', 'index.php?trg=ontoedit&amp;act=vrml&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."'", $btn2);
    //2D-view
    $btn3 = str_replace($optmatches[0], "", $datmatches[1]);
    $btn3 = str_replace("%TYPE%", "Button", $btn3);
    $btn3 = str_replace("%NODE_ID%", "", $btn3);
    $btn3 = str_replace("%NODE_TL%", getMessage("2D-view"), $btn3);
    $btn3 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;act=2dview&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."'", $btn3);
    //Div
    $btns = str_replace($datmatches[0], $btn1.$btn2.$btn3, $matches[1]);
    $btns = str_replace("%GROUP_ID%", "1", $btns);
    $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);
  }
  if ($data["type"] >= 1) { //user
    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Text", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Text", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //Type
    //options
    $opts = "";
    for ($i = 0; $i < $i_max; $i++) {
      $opt = str_replace("%OPT_ID%", $ontotypes[$i]["id"], $optmatches[1]);
      $opt = str_replace("%OPT_TL%", $ontotypes[$i]["name"], $opt);
      if ($ontodata["type"] == $ontotypes[$i]["id"])
        $opt = str_replace("%SELECTED%", "selected", $opt);
      else
        $opt = str_replace("%SELECTED%", "0", $opt);
      $opts .= $opt;
    }
    //type
    $utype = str_replace($optmatches[0], $opts, $datmatches[1]);
    $utype = str_replace("%TYPE%", "Option", $utype);
    $utype = str_replace("%NODE_ID%", "otype", $utype);
    $utype = str_replace("%NODE_TL%", getMessage("Type"), $utype);
    //Label
    $lablttt = str_replace($optmatches[0], "", $datmatches[1]);
    $lablttt = str_replace("%TYPE%", "Label", $lablttt);
    $lablttt = str_replace("%NODE_ID%", "", $lablttt);
    if ($comment != "") $lablttt = str_replace("%NODE_TL%", getMessage("Error"), $lablttt);
    else $lablttt = str_replace("%NODE_TL%", "", $lablttt);
    //$lablttt = str_replace("%VALUE%", $userdata["type"], $lablttt);
    $lablttt = str_replace("%VALUE%", $comment, $lablttt);
    //button save
    $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
    $ubutn = str_replace("%TYPE%", "Button", $ubutn);
    $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
    $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
    $ubutn = str_replace("%VALUE%", "'0','oname','odescr','otype'", $ubutn);



    $udata = str_replace($datmatches[0], $fname.$lname.$utype.$lablttt.$ubutn, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Ontology data"), $udata);
    //print "****".$udata."****";
  }
  else { //guest
    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Label", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Label", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //Type
    //options
    $opts = "";
    for ($i = 0; $i < $i_max; $i++) {
      if ($ontodata["type"] == $ontotypes[$i]["id"]) break;
    }
    //type
    $utype = str_replace($optmatches[0], "", $datmatches[1]);
    $utype = str_replace("%TYPE%", "Label", $utype);
    $utype = str_replace("%NODE_ID%", "otype", $utype);
    $utype = str_replace("%NODE_TL%", getMessage("Type"), $utype);
    $utype = str_replace("%VALUE%", $ontotypes[$i]["name"], $utype);
    //User
    $udata = str_replace($datmatches[0], $fname.$lname.$utype, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Ontology data"), $udata);
    //print "****".$udata."****";
  }
  return str_replace($matches[0], $udata.$chks.$btns, $contents);
}
?>